/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.dao.hibernate;

import com.minierp.dao.MessageDao;
import com.minierp.dao.hibernate.BaseDaoImpl;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

public class MessageDaoImpl extends BaseDaoImpl implements MessageDao {
	public int delete(final long id) {
		Object obj = this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						String hql = "delete com.minierp.model.Message where id = :id";
						return new Integer(session.createQuery(hql)
								.setLong("id", id).executeUpdate());
					}
				});
		return ((Integer) obj).intValue();
	}

	public int deleteReceivers(final long messageId) {
		Object obj = this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						String hql = "delete com.minierp.model.MessageReceiver where messageId = :messageId";
						return new Integer(session.createQuery(hql)
								.setLong("messageId", messageId)
								.executeUpdate());
					}
				});
		return ((Integer) obj).intValue();
	}

	public List<?> getTopInList(long receiverId, int num) {
		String sql = "select a,r from com.minierp.model.Message a,com.minierp.model.MessageReceiver r where a.id=r.messageId and r.receiverId="
				+ receiverId
				+ " and r.receiverDelete=\'N\'"
				+ " and r.receiverRead=\'N\'" + " order by a.id desc";
		Session sess = null;

		List arg7;
		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			q.setFirstResult(0);
			q.setMaxResults(num);
			arg7 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg7;
	}

	public List<?> getReceiverList(long messageId) {
		String sql = "from com.minierp.model.MessageReceiver r where r.messageId="
				+ messageId + " order by r.id";
		return this.getHibernateTemplate().find(sql);
	}

	public int updateReadFlag(final long messageId, final long receiverId) {
		Object obj = this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						String hql = "update com.minierp.model.MessageReceiver set receiverRead=\'Y\' where messageId = :messageId and receiverId=:receiverId";
						return new Integer(session.createQuery(hql)
								.setLong("messageId", messageId)
								.setLong("receiverId", receiverId)
								.executeUpdate());
					}
				});
		return ((Integer) obj).intValue();
	}

	public int updateReadFlag(final long receiverId) {
		Object obj = this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						String hql = "update com.minierp.model.MessageReceiver set receiverRead=\'Y\' where receiverId=:receiverId";
						return new Integer(session.createQuery(hql)
								.setLong("receiverId", receiverId)
								.executeUpdate());
					}
				});
		return ((Integer) obj).intValue();
	}

	public int getNewMessageCount(long receiverId) {
		String sql = "select count(id) from com.minierp.model.MessageReceiver r where r.receiverDelete=\'N\' and r.receiverRead=\'N\' and r.receiverId="
				+ receiverId;
		List list = this.getHibernateTemplate().find(sql);
		return list.isEmpty() ? 0 : Integer.parseInt(list.get(0).toString());
	}

	public int getMessageCount(long relationId, long receiverId) {
		String sql = "select count(m.id) from  com.minierp.model.Message m,com.minierp.model.MessageReceiver r where m.id=r.messageId and m.relationId="
				+ relationId
				+ " and r.receiverId="
				+ receiverId
				+ " and r.receiverDelete=\'N\'";
		List list = this.getHibernateTemplate().find(sql);
		return list.isEmpty() ? 0 : Integer.parseInt(list.get(0).toString());
	}

	public List<?> getRelationList(long relationId) {
		String sql = "from com.minierp.model.Message a where a.relationId="
				+ relationId + " order by a.id desc";
		return this.getHibernateTemplate().find(sql);
	}

	public List getAppList(long receiverId, int startIndex, int num)
			throws Exception {
		String sql = "select a,r.receiverRead from com.minierp.model.Message a,com.minierp.model.MessageReceiver r where a.id=r.messageId and r.receiverId="
				+ receiverId
				+ " and r.receiverDelete=\'N\'"
				+ " order by a.id desc";
		Session sess = null;

		List arg8;
		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			q.setFirstResult(startIndex);
			q.setMaxResults(num);
			arg8 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg8;
	}

	public int getAppListCount(long receiverId) throws Exception {
		String sql = "select count(*) from com.minierp.model.Message a,com.minierp.model.MessageReceiver r where a.id=r.messageId and r.receiverId="
				+ receiverId
				+ " and r.receiverDelete=\'N\'"
				+ " order by a.id desc";
		Session sess = null;

		int arg7;
		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			List list = q.list();
			if (list.isEmpty() || list.get(0) == null) {
				return 0;
			}

			arg7 = Integer.parseInt(list.get(0).toString());
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg7;
	}
}